const db = require("../config/mysql_util");
class LogsModel {
  constructor() {}
  getCount(data) {
    let sql = `select count(*) count from logs l inner join users u on u.user_id = l.user_id where 1 `;
    if (data.user_name) sql += ` and user_name like '%${data.user_name}%' `;
    if (data.user_id) sql += ` and l.user_id = ${data.user_id} `;
    return db.query(sql);
  }

  getAll(data) {
    let limit = data.pagesize * (data.page - 1);
    let sql = ` select l.*, user_name from logs l inner join users u on u.user_id = l.user_id where 1 `;
    if (data.user_name) sql += ` and user_name like '%${data.user_name}%' `;
    if (data.user_id) sql += ` and l.user_id = ${data.user_id} `;
    sql += ` order by id desc limit ${limit}, ${data.pagesize} `;
    return db.query(sql);
  }

  insert(data) {
    return db.query("insert into logs set ?", [data]);
  }
}

module.exports = new LogsModel();